package com.itheima.youdianda.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.youdianda.common.R;
import com.itheima.youdianda.common.ResultData;
import com.itheima.youdianda.entity.YddCate;
import com.itheima.youdianda.service.YddCateService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {

    @Autowired
    private YddCateService yddCateService;

    /**
     * 新增
     *
     * @param yddCate
     */
    @PostMapping("/add")
    public R add(@RequestBody YddCate yddCate) {
        yddCateService.save(yddCate);
        return R.success("", "添加成功");
    }


    /**
     * 分页展示
     *
     * @param catename
     * @param page
     * @param limit
     */
    @GetMapping("/list")
    public ResultData list(String catename, int page, int limit) {
        //新建分页以及查询构造器
        Page<YddCate> yddCatePage = new Page<>(page, limit);
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(catename), YddCate::getCatename, catename);
        Page<YddCate> pageInfo = yddCateService.page(yddCatePage, queryWrapper);

        //将数据进行封装
        HashMap<String, Object> map = new HashMap<>();
        map.put("count", (int) pageInfo.getTotal());
        map.put("totalPages", (int) pageInfo.getPages());
        map.put("pageSize", (int) limit);
        map.put("currentPage", (int) pageInfo.getCurrent());
        map.put("data", pageInfo.getRecords());
        //返回执行结果
        return ResultData.ok("", map);
    }

    /**
     * 修改数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(int id) {
        YddCate yddCate = yddCateService.getById(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", yddCate.getId());
        map.put("catename", yddCate.getCatename());
        map.put("sort_num", yddCate.getSortNum());
        map.put("icon", yddCate.getIcon());
        return ResultData.ok("", map);
    }


    /**
     * 分类管理
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(int id) {
        yddCateService.removeById(id);
        return ResultData.ok("删除成功", "");
    }

    /**
     * 修改数据
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate) {
        log.info("接收到的数据是: {}", yddCate);
        LambdaUpdateWrapper<YddCate> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(YddCate::getId, yddCate.getId());
        yddCateService.update(yddCate, updateWrapper);
        return ResultData.ok("修改成功", "");
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        List<YddCate> list = yddCateService.list();
        return ResultData.ok("分类所有数据", list);
    }

}
